package main

import (
	"github.com/micro/go-micro/registry"
	"github.com/micro/go-micro/util/log"
	"github.com/micro/go-micro"
	"github.com/micro/go-plugins/registry/consul"
	"shopping/user/db"
	"shopping/user/handler"
	"shopping/user/model"
	"shopping/user/repository"

	user "shopping/user/proto/user"
)

func main() {
	defer db.MasterDb.Close()
	db.MasterDb.AutoMigrate(&model.User{})

	repo:= &repository.User{db.MasterDb}

	reg:= consul.NewRegistry(func(op *registry.Options) {
		op.Addrs = []string{
			"127.0.0.1:8500",
		}

	})

	// New Service
	service := micro.NewService(
		micro.Registry(reg),
		micro.Name("go.micro.srv.user"),
		micro.Version("latest"),
	)

	// Initialise service
	service.Init()

	// Register Handler
	user.RegisterUserServiceHandler(service.Server(), &handler.User{repo})

	// Run service
	if err := service.Run(); err != nil {
		log.Fatal(err)
	}
}
